class SearchController < ApplicationController
def results

      @university_id           = University.find_by_stripped_name(params[:university]).id
      @university          = University.find_by_stripped_name(params[:university])
             
     if (params[:keyword])!=''
             search_term           = params[:keyword]
             search_term           = search_term.split(" ").collect{|x| "title like '%#{x}%' or description like '%#{x}%' "}.join(" or ")
             if !(params[:category]).nil?
               @sub_categories      = Category.find_by_stripped_name(params[:category]).sub_categories
              
                @sub_categories.each do |s|
                @extra_conditions= " sub_category_id = "+s.id.to_s+" or "
               end
               @extra_conditions=@extra_conditions + "sub_category_id =99 "
               @advertisments        = Advertisment.paginate :page => params[:page],:per_page => 10,:conditions =>"("+search_term+ ") and ("+@extra_conditions+") and university_id="+@university_id.to_s
             elsif !(params[:sub_category]).nil?
               @advertisments        = Advertisment.paginate :page => params[:page],:per_page => 10,:conditions =>"sub_category_id="+SubCategory.find_by_stripped_name(params[:sub_category]).id.to_s+" and university_id="+@university_id.to_s
             
            else
               @advertisments        = Advertisment.paginate :page => params[:page],:per_page => 10,:conditions =>"("+search_term+ ") and university_id="+@university_id.to_s
             end
             
             @total_ads            = @advertisments.total_entries
             @categories           = Category.find(:all,:order=>"name")




#          @original_category=params[:category]
#          @original_sub_category=(params[:sub_category])
#          @original_university = (params[:university])
#          @location=params[:location]
#          search_term = params[:keyword]
#          @search_term=search_term
#          @category = Category.find_by_stripped_name(params[:category])
#          @sub_category=SubCategory.find_by_stripped_name(params[:sub_category])
#          @university = University.find_by_stripped_name(params[:university])
#          if (params[:keyword])!=''
#            search_term = search_term.split(" ").collect{|x| "title like '%#{x}%' or description like '%#{x}%' "}.join(" or ")
#            @search_term1 = "("+ search_term + ") and (sub_category_id="+@sub_category.id.to_s+" and university_id="+@university.id.to_s+"  and location like '%"+@location.to_s+"%')"
#          else
#            @search_term1 = "sub_category_id="+@sub_category.id.to_s+" and university_id="+@university.id.to_s+"  and location like '%"+@location.to_s+"%'"
#          end
#
    end
        respond_to do |format|
        format.html
        format.js {
                      render :update do |page|
                      page.replace_html 'listings-search-results', :partial => 'search_results'
                      end
                  }
         end




end
end